package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthSchoolExpertFileService;
import com.orange.modules.entity.AuthSchoolExpertFile;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/SchoolExpertFile")
public class AuthSchoolExpertFileController extends BaseController{
	
	private static final Logger log = Logger.getLogger(AuthSchoolExpertFileController.class);
	
	@Autowired
	private AuthSchoolExpertFileService authSchoolExpertFileService;
	
	/**
	 * 保存专家传的文件
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertAuthSchoolExpertFile" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertAuthSchoolExpertFile(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String sid = map.get("sid");
			String expertId = map.get("expertid");
			String filePath = map.get("filepath");
			String filename = map.get("filename");
			if(sid==null || StringUtils.isBlank(sid))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校ID不能为空");
			if(expertId==null || StringUtils.isBlank(expertId))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"专家ID不能为空");
			if(filePath==null || StringUtils.isBlank(filePath))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"文件路径不能为空");
			if(filename==null || StringUtils.isBlank(filename))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"文件名不能为空");
			List<AuthSchoolExpertFile> list = authSchoolExpertFileService.queryFileByschoolIdExpertId(sid, expertId);
			if(list.size()>0){
				for(AuthSchoolExpertFile aa : list){
					authSchoolExpertFileService.deleteAuthSchoolExpertFile(aa.getPid());
				}
			}
			AuthSchoolExpertFile as = new AuthSchoolExpertFile();
			as.setCreatetime(sdf.format(new Date()));
			as.setExpertId(expertId);
			as.setFilename(filename);
			as.setFilepath(filePath);
			as.setSid(sid);
			authSchoolExpertFileService.insertAuthSchoolExpertFile(as);
			return toResponse(as,ReturnCode.SUCCESS,"新增成功");
		} catch (Exception e) {
			// TODO: handle exception
			return toResponse(null,ReturnCode.FAIL,"新增失败");
		}
		
	}
	
	/**
	 * 查询专家上传的文件
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthSchoolExpertFile" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolExpertFile(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String sid = map.get("sid");
		String expertid = map.get("expertid");
		List<AuthSchoolExpertFile> list = authSchoolExpertFileService.queryFileByschoolIdExpertId(sid, expertid);
		return toResponse(list,ReturnCode.SUCCESS,"查询成功");
	}
	
}
